<?php
@header('Content-Type: text/html; charset=utf-8');
include_once('../bakend/register.php');

function do_reg($email, $keywords, $board, $reply_limit)
{
  smth_register($email, $keywords, $board, $reply_limit);
  if (smth_check_keywords($email, $keywords, $board, $reply_limit))
  {
    if (is_new_keywords($email, $board))
    {
      echo "[$email] 关键词 [$board][$keywords] 订阅成功！\n";
      dispatch_one_user($email);
    }
  }
}

function handle_stdin()
{
  $file = fopen("php://stdin", "r");
  if ($file)
  {
    while ($line = fgets($file))
    {
      $obj = json_decode($line);
      if (isset($obj->email)
          && isset($obj->keywords)
          && isset($obj->board))
      {
        $email = $obj->email;
        $keywords = $obj->keywords;
        $board = $obj->board;
        $reply_limit = 0;
        if (isset($obj->reply_limit))
        {
          $reply_limit = $obj->reply_limit;
        }
        printf("[%s] start register\n", date("Y-m-d H:i:s"));
        print_r($obj);
        do_reg($email, $keywords, $board, $reply_limit);
      }
    }
  }
  fclose($file);
}

handle_stdin();

?>
